// import Business from '@/assets/demoIcon/business.png';
// import BusinessA from '@/assets/demoIcon/businessA.png';
import client from '@/assets/demoIcon/client.png';
import clientA from '@/assets/demoIcon/clientA.png';
// import Finance from '@/assets/demoIcon/finance.png';
// import FinanceA from '@/assets/demoIcon/financeA.png';
import HomeGary from '@/assets/demoIcon/home.png';
import HomeBlue from '@/assets/demoIcon/home1.png';
import listPng from '@/assets/demoIcon/list.png';
import selectedListPng from '@/assets/demoIcon/list1.png';

import diagnosisGroup from '@/assets/demoIcon/diagnosisGroup.png';
import diagnosisGroupA from '@/assets/demoIcon/diagnosisGroupA.png';

// import Project from '@/assets/demoIcon/project.png';
// import ProjectA from '@/assets/demoIcon/projectA.png';
import { setTabBarList } from 'alita';
import { useCallback, useState } from 'react';

import { type ComplexOAUser, WeiXinUser } from '@/pages/types/type';


export declare type UseAuthModel = {
  userInfo: ComplexOAUser,
  admin: boolean,
  signin: (user: ComplexOAUser) => void,
  hasOaSystemRole: (role: string) => boolean,
  anyOaSystemRoles: (roles: string[]) => boolean,
  sameAs: (u: WeiXinUser) => boolean,
  justADoctor: boolean
}

export default function useAuthModel(): UseAuthModel {

  const [userInfo, setUserInfo] = useState<ComplexOAUser>({
    avatar: 'avatar',
    id: 'd',
    name: '魔戒',
    oaSystemLivings: [],
    diagnosisGroups: [],
    projectManager: false,
    manageHospitals: []
  });

  const signin = useCallback((u: ComplexOAUser) => {
    setUserInfo(u);

    if (u.manageHospitals.length > 0) {
      console.log(`${u.name} manageHospitals!!! `);
      setTabBarList([
        {
          pagePath: '/study/search',
          text: '单据列表',
          iconPath: listPng,
          selectedIconPath: selectedListPng
        },
        {
          pagePath: '/',
          text: '医院端'
        },
        {
          pagePath: '/backend/management',
          text: '管理',
          remove: false
        },
        {
          pagePath: '/backend/myself',
          text: '我的',
          iconPath: client,
          selectedIconPath: clientA,
          remove: false
        },
        {
          pagePath: '/diagnosis-group/search',
          remove: true
        },
      ]);
    }

    if (u.diagnosisGroups.length > 0) {
      console.log(`${u.name} diagnosisGroups!!! `);
      setTabBarList([
        {
          pagePath: '/study/search',
          text: '单据列表',
          iconPath: listPng,
          selectedIconPath: selectedListPng,
          remove: true
        },
        {
          pagePath: '/',
          text: '单据列表',
          iconPath: listPng,
          selectedIconPath: selectedListPng
        },
        {
          pagePath: '/backend/myself',
          text: '我的',
          iconPath: client,
          selectedIconPath: clientA,
          remove: false
        },

        {
          pagePath: '/backend/management',
          text: '管理',
          remove: true,
        },
        {
          pagePath: '/diagnosis-group/search',
          text: '诊断组',
          iconPath: diagnosisGroup,
          selectedIconPath: diagnosisGroupA,
          title: '诊断组',
          iconSize: '',
          badge: '',
        },
      ]);
    }
    if (u.doctor) {
      console.log(`${u.name} is a doctor!!! `);
      setTabBarList([
        {
          pagePath: '/study/search',
          text: '单据列表',
          iconPath: listPng,
          selectedIconPath: selectedListPng,
          remove: true
        },
        {
          pagePath: '/',
          text: '申请列表',
          iconPath: listPng,
          selectedIconPath: selectedListPng,
          remove: false
        },
        {
          pagePath: '/backend/myself',
          text: '我的',
          iconPath: client,
          selectedIconPath: clientA,
          remove: false
        },
        {
          pagePath: '/backend/management',
          text: '管理',
          remove: true,
        },
        {
          pagePath: '/diagnosis-group/search',
          remove: true
        },
      ]);
    }


  }, []);

  return {
    userInfo,
    signin,
    admin: userInfo && userInfo?.oaSystemLivings.some(l => l.type.standard.name === 'projectManager') || false,
    justADoctor: userInfo !== undefined && userInfo?.diagnosisGroups.length < 1 && userInfo?.doctor !== undefined,
    hasOaSystemRole: (s) => userInfo?.oaSystemLivings.some(l => l.type.standard.name === s) || false,
    anyOaSystemRoles: (roles) => userInfo?.oaSystemLivings.some(l => roles.some(role => l.type.standard.name === role)) || false,
    sameAs: u => userInfo?.id === u.userid
  };
}
